package com.mychat.utils;

import com.mychat.enums.UserContactTypeEnum;
import org.apache.commons.codec.digest.DigestUtils;
import org.apache.commons.lang3.RandomStringUtils;

import java.util.Arrays;

public class StringUtils {

    public static Boolean isEmpty(String str) {
        if (null == str || "".equals(str.trim()) || "null".equals(str)) {
            return true;
        } else {
            return false;
        }
    }

    public static final String getUserId() {
        return UserContactTypeEnum.USER.getPrefix() + getRandomNumber(11);
    }
    public static final String getGroupId() {
        return UserContactTypeEnum.GROUP.getPrefix() + getRandomNumber(11);
    }

    public static final String getRandomNumber(Integer count) {
        return RandomStringUtils.random(count, false, true);
    }

    public static final String getRandomString(Integer count) {
        return RandomStringUtils.random(count, true, true);
    }

    public static String encodeMd5(String sourceStr) {
        return StringUtils.isEmpty(sourceStr) ? null : DigestUtils.md5Hex(sourceStr);
    }

    public static String getSuffix(String str) {
        return str.substring(str.lastIndexOf("."));
    }

    public static String getFileSuffix(String filename) {
        if(isEmpty(filename)) {
            return null;
        }
        return filename.substring(filename.lastIndexOf("."));
    }


    public static String cleanHtmlTag(String content) {
        if(isEmpty(content)) {
            return content;
        }
        content = content.replace("<", "&lt;");
        content = content.replace("\r\n", "<br>");
        content = content.replace("\n", "<br>");
        return content;
    }

    public static final String getChatSessionId4User(String[] userIds) {
        Arrays.sort(userIds);
        return encodeMd5(org.apache.commons.lang3.StringUtils.join(userIds, ""));
    }

    public static boolean isNumber(String str) {
        String checkNumber = "^[0-9]+$";
        if(null == str) {
            return false;
        }
        return str.matches(checkNumber);
    }
}
